package com.redrose.flutterdemo.utils;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池管理类
 */
public class ThreadPoolUtil {
    private static ExecutorService mThreadPool;

    static {
        mThreadPool = Executors.newFixedThreadPool(10);

    }

    public static ExecutorService getThreadPool() {
        return mThreadPool;
    }

    private ThreadPoolUtil() {
    }

    private static Handler sHandler = new Handler(Looper.getMainLooper());



    /**
     * 在子线程执行任务
     *
     * @param task
     */
    public static void runOnWorkThread(Runnable task) {
        mThreadPool.submit(task);
    }

    /**
     * 在UI线程执行任务
     *
     * @param task
     */
    public static void runOnMainThread(Runnable task) {
        sHandler.post(task);
    }

    /**
     * 在UI线程延时执行任务
     *
     * @param task
     * @param delayMillis 延时时间，单位毫秒
     */
    public static void runOnMainThread(Runnable task, long delayMillis) {
        sHandler.postDelayed(task, delayMillis);
    }
}
